package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.CompanyPurchaseProjectsAdapter
import com.hinnotek.amoeba.bean.old.CompanyPurchaseByCompanyPurchaseIdBean
import com.hinnotek.amoeba.databinding.ActivityPuachaseDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaCustomerViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PurchaseDetailActivity : BaseActivity<ActivityPuachaseDetailBinding>() {

    private var adapter: CompanyPurchaseProjectsAdapter? = null
    private val purchaseViewModel by viewModels<AmibaCustomerViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_puachase_detail)
        setLeftVisible(true)
        setTitle("采购")

    }

    override fun initVew() {
        val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        bindingView.rlvProduct.setLayoutManager(manager)
        bindingView.rlvProduct.setAdapter(CompanyPurchaseProjectsAdapter().also { adapter = it })
    }

    override fun setData() {
        val companyPurchaseId = intent.getIntExtra("companyPurchaseId", 0)
        getCompanyPurchaseByCompanyPurchaseId(companyPurchaseId)
    }

    /**
     * app端通过id查询单个详情接口
     */
    private fun getCompanyPurchaseByCompanyPurchaseId(companyPurchaseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        purchaseViewModel.getCompanyPurchaseByCompanyPurchaseId(companyPurchaseId)
            .observe(this, object :
                ObserverState<CompanyPurchaseByCompanyPurchaseIdBean>() {
                override fun onDataChange(data: CompanyPurchaseByCompanyPurchaseIdBean) {
                    bindingView.tvItemPurchaseType.text =
                        if (data.companyPurchaseType == 1) "产品" else "项目"
                    bindingView.tvPurchaseAmoeba.text = data.orgName
                    if (data.companyPurchaseType == 1) {
                        bindingView.tvPurchaseProject.text = data.provideProjectName
                    } else {
                        bindingView.tvPurchaseCustomer.text = data.customerName
                        bindingView.tvPurchaseProject.text = data.projectName
                        bindingView.llChooseCustom.visibility = View.VISIBLE
                    }
                    bindingView.tvPurchaseMoney.text = data.purchaseMoney
                    bindingView.tvPurchaseApprover.text = data.approvalUser
                    adapter?.setList(data.companyPurchaseProjects)
                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }
}